<?php
/**
 * Created by PhpStorm.
 * User: Bolot
 * Date: 05.12.13
 * Time: 16:32
 */
defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Categories extends Controller_Admin_Admin {
  public function action_index()
  {
    $this->template->title = 'Админ-Категории';
    $categoryTable = new Model_Category();
    $categories = $categoryTable->find_all();
    $this->template->content = View::factory('admin/categories/index', array('categories' => $categories));
  }

  public function action_create()
  {
    $this->template->title = 'Админ-Категории';
    if ($post = $this->request->post()) {
      $category = new Model_Category();
      $category->title = $post['title'];
      $category->parent_id = $post['parent_id'];
      $category->description = $post['description'];
      $category->save();
      Controller::redirect('admin/categories');
    }
    $cat = new Model_Category();
    $cats = $cat->where('parent_id', '=', 0)->find_all();
    $categories = array();
    $categories[0] = '';
    foreach ($cats as $c) {
      $categories[$c->id] = $c->title;
    }

    $this->template->content = View::factory('admin/categories/create', array('categories' => $categories));
  }

  public function action_edit()
  {
    $this->template->title = 'Админ-Категории';
    $category_id = $this->request->param('id');
    $category = ORM::factory('Category', $category_id);
    if ($post = $this->request->post()) {
      $category->title = $post['title'];
      $category->parent_id = $post['parent_id'];
      $category->description = $post['description'];
      $category->save();
      Controller::redirect('admin/categories');
    }
    $cat = new Model_Category();
    $cats = $cat->where('parent_id', '=', 0)->find_all();
    $categories = array();
    $categories[0] = '';
    foreach ($cats as $c) {
      if ($c->id != $category->id)
      $categories[$c->id] = $c->title;
    }

    $this->template->content = View::factory('admin/categories/edit', array('category' => $category, 'categories' => $categories));
  }

  public function action_delete()
  {
    $this->template->title = 'Админ-Категории';
    $category_id = $this->request->param('id');
    $category = ORM::factory('Category', $category_id);
    if ($post = $this->request->post()) {
      $category->delete();
      while($child = ORM::factory('Category')->where('parent_id', '=', $category_id)->find()) {
        if ($child->id) {
          $child->delete();
        } else {
          break;
        }
      }
      Controller::redirect('admin/categories');
    }
    $this->template->content = View::factory('admin/categories/delete', array('category' => $category));
  }

  public function action_getajax()
  {
    $child_categories = array();
    $category_id = $this->request->post('category_id');
    if ($category_id) {
      $child_categories = ORM::factory('Category')->where('parent_id', '=', $category_id)->find_all();
    }
    $categories_array = array();
    $categories_array[] = '';
    foreach($child_categories as $child) {
      $categories_array[$child->id] = $child->title;
    }
    if (count($categories_array) == 1) $categories_array = array();
    $this->request->headers('Content-type','application/json; charset='.Kohana::$charset);
    $this->response->body(json_encode(array('categories' => $categories_array)));
  }
}